<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一、环境准备    🔗在Ubuntu中安装docker    二、主机1、环境搭建1.1 镜像拉取1docker pull ubuntu:16.04 1.2 创建网桥1docker network create -d&#x3D;bridge --subnet&#x3D;192.168.126.0&#x2F;24 br1 1.3 启动容器1docker run -it --name ubuntu-1 --privileged">
<meta property="og:type" content="article">
<meta property="og:title" content="docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程">
<meta property="og:url" content="http://lxcwlong.gitee.io/lxcwlong/2023/12/25/docker-ubuntu%E4%B8%AD%E5%9F%BA%E4%BA%8Ekeepalived+niginx%E6%A8%A1%E6%8B%9F%E4%B8%BB%E4%BB%8E%E7%83%AD%E5%A4%87%E5%AE%8C%E6%95%B4%E8%BF%87%E7%A8%8B/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="一、环境准备    🔗在Ubuntu中安装docker    二、主机1、环境搭建1.1 镜像拉取1docker pull ubuntu:16.04 1.2 创建网桥1docker network create -d&#x3D;bridge --subnet&#x3D;192.168.126.0&#x2F;24 br1 1.3 启动容器1docker run -it --name ubuntu-1 --privileged">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://img-blog.csdnimg.cn/direct/d461438f337544b586c9060a8f7dda3d.png#pic_center">
<meta property="article:published_time" content="2023-12-25T06:18:18.606Z">
<meta property="article:modified_time" content="2023-12-25T06:29:12.753Z">
<meta property="article:author" content="Wcloong">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/direct/d461438f337544b586c9060a8f7dda3d.png#pic_center">
  
    <link rel="alternate" href="/lxcwlong/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/lxcwlong/favicon.png">
  
  
  
<link rel="stylesheet" href="/lxcwlong/css/style.css">

  
    
<link rel="stylesheet" href="/lxcwlong/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 7.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/lxcwlong/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/lxcwlong/">Home</a>
        
          <a class="main-nav-link" href="/lxcwlong/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/lxcwlong/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://lxcwlong.gitee.io/lxcwlong"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/lxcwlong/2023/12/25/docker-ubuntu%E4%B8%AD%E5%9F%BA%E4%BA%8Ekeepalived+niginx%E6%A8%A1%E6%8B%9F%E4%B8%BB%E4%BB%8E%E7%83%AD%E5%A4%87%E5%AE%8C%E6%95%B4%E8%BF%87%E7%A8%8B/" class="article-date">
  <time class="dt-published" datetime="2023-12-25T06:18:18.606Z" itemprop="datePublished">2023-12-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="一、环境准备"><a href="#一、环境准备" class="headerlink" title="一、环境准备"></a>一、环境准备</h1><table cellpadding=10><tr><td bgcolor=#efefedf align="left" line-height=5em;><font face="" size=2 color="">
<b>
<a target="_blank" rel="noopener" href="https://blog.csdn.net/lxcw_sir/article/details/134708046?spm=1001.2014.3001.5502">
<font color="blue"><u>
🔗在Ubuntu中安装docker
</u></font></a>
</font></td></tr></table>

<h1 id="二、主机"><a href="#二、主机" class="headerlink" title="二、主机"></a>二、主机</h1><h2 id="1、环境搭建"><a href="#1、环境搭建" class="headerlink" title="1、环境搭建"></a>1、环境搭建</h2><h3 id="1-1-镜像拉取"><a href="#1-1-镜像拉取" class="headerlink" title="1.1 镜像拉取"></a>1.1 镜像拉取</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull ubuntu:16.04</span><br></pre></td></tr></table></figure>
<h3 id="1-2-创建网桥"><a href="#1-2-创建网桥" class="headerlink" title="1.2 创建网桥"></a>1.2 创建网桥</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker network create -d=bridge --subnet=192.168.126.0/24 br1</span><br></pre></td></tr></table></figure>
<h3 id="1-3-启动容器"><a href="#1-3-启动容器" class="headerlink" title="1.3 启动容器"></a>1.3 启动容器</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name ubuntu-1 --privileged -v /home/vac/linux:/mnt/software -p 8801:80 --net=br1 ubuntu:16.04 bash</span><br></pre></td></tr></table></figure>
<h3 id="1-4-下载工具包"><a href="#1-4-下载工具包" class="headerlink" title="1.4 下载工具包"></a>1.4 下载工具包</h3><p>先更新以下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt update</span><br></pre></td></tr></table></figure>

<p>ifconfig、route命令使用的net-tools工具包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt -y install net-tools</span><br></pre></td></tr></table></figure>
<p>vim编辑器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt -y install vim</span><br></pre></td></tr></table></figure>
<p>ping命令工具包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt -y install iputils-ping</span><br></pre></td></tr></table></figure>

<h3 id="1-5-下载nginx"><a href="#1-5-下载nginx" class="headerlink" title="1.5 下载nginx"></a>1.5 下载nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt -y install nginx</span><br></pre></td></tr></table></figure>
<h3 id="1-6-下载keepalived"><a href="#1-6-下载keepalived" class="headerlink" title="1.6 下载keepalived"></a>1.6 下载keepalived</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt -y install keepalived</span><br></pre></td></tr></table></figure>


<h2 id="2、配置"><a href="#2、配置" class="headerlink" title="2、配置"></a>2、配置</h2><h3 id="2-1-配置keepalived"><a href="#2-1-配置keepalived" class="headerlink" title="2.1 配置keepalived"></a>2.1 配置keepalived</h3><p>编辑keepalived.conf文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/keepalived/keepalived.conf</span><br></pre></td></tr></table></figure>
<p>输入以下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">! Configuration File <span class="keyword">for</span> keepalived</span><br><span class="line"></span><br><span class="line">global_defs &#123;</span><br><span class="line">   <span class="comment">#路由id：当前安装keepalived节点主机的标识符，全局唯一</span></span><br><span class="line">   router_id keep_130</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    <span class="comment"># 表示的状态，当前的130服务器为nginx的主节点，MASTER/BACKUP</span></span><br><span class="line">    state MASTER</span><br><span class="line">    <span class="comment"># 当前实例绑定的网卡</span></span><br><span class="line">    interface eth0</span><br><span class="line">    <span class="comment"># 保证主备节点一致</span></span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    <span class="comment"># 优先级/权重，谁的优先级高，在MASTER挂掉以后，就能成为MASTER</span></span><br><span class="line">    priority 100</span><br><span class="line">    <span class="comment"># 主备之间同步检查的时间间隔，默认1s</span></span><br><span class="line">    advert_int 1</span><br><span class="line">    <span class="comment"># 认证授权的密码，防止非法节点的进入</span></span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.200.16</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-2-配置nginx"><a href="#2-2-配置nginx" class="headerlink" title="2.2 配置nginx"></a>2.2 配置nginx</h3><h4 id="2-2-1-查看nginx-conf"><a href="#2-2-1-查看nginx-conf" class="headerlink" title="2.2.1 查看nginx.conf"></a>2.2.1 查看nginx.conf</h4><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/nginx/nginx.conf</span><br></pre></td></tr></table></figure>
<p>内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">user www-data;</span><br><span class="line">worker_processes auto;</span><br><span class="line">pid /run/nginx.pid;</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">        worker_connections 768;</span><br><span class="line">        <span class="comment"># multi_accept on;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line">        <span class="comment"># Basic Settings</span></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line"></span><br><span class="line">        sendfile on;</span><br><span class="line">        tcp_nopush on;</span><br><span class="line">        tcp_nodelay on;</span><br><span class="line">        keepalive_timeout 65;</span><br><span class="line">        types_hash_max_size 2048;</span><br><span class="line">        <span class="comment"># server_tokens off;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># server_names_hash_bucket_size 64;</span></span><br><span class="line">        <span class="comment"># server_name_in_redirect off;</span></span><br><span class="line"></span><br><span class="line">        include /etc/nginx/mime.types;</span><br><span class="line">        default_type application/octet-stream;</span><br><span class="line"></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line">        <span class="comment"># SSL Settings</span></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line"></span><br><span class="line">        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; <span class="comment"># Dropping SSLv3, ref: POODLE</span></span><br><span class="line">        ssl_prefer_server_ciphers on;</span><br><span class="line"></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line">        <span class="comment"># Logging Settings</span></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line"></span><br><span class="line">        access_log /var/log/nginx/access.log;</span><br><span class="line">        error_log /var/log/nginx/error.log;</span><br><span class="line"></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line">        <span class="comment"># Gzip Settings</span></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line"></span><br><span class="line">        gzip on;</span><br><span class="line">        gzip_disable <span class="string">&quot;msie6&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment"># gzip_vary on;</span></span><br><span class="line">        <span class="comment"># gzip_proxied any;</span></span><br><span class="line">        <span class="comment"># gzip_comp_level 6;</span></span><br><span class="line">        <span class="comment"># gzip_buffers 16 8k;</span></span><br><span class="line">        <span class="comment"># gzip_http_version 1.1;</span></span><br><span class="line">        <span class="comment"># gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line">        <span class="comment"># Virtual Host Configs</span></span><br><span class="line">        <span class="comment">##</span></span><br><span class="line"></span><br><span class="line">        include /etc/nginx/conf.d/*.conf;</span><br><span class="line">        include /etc/nginx/sites-enabled/*;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#mail &#123;</span></span><br><span class="line"><span class="comment">#       # See sample authentication script at:</span></span><br><span class="line"><span class="comment">#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript</span></span><br><span class="line"><span class="comment"># </span></span><br><span class="line"><span class="comment">#       # auth_http localhost/auth.php;</span></span><br><span class="line"><span class="comment">#       # pop3_capabilities &quot;TOP&quot; &quot;USER&quot;;</span></span><br><span class="line"><span class="comment">#       # imap_capabilities &quot;IMAP4rev1&quot; &quot;UIDPLUS&quot;;</span></span><br><span class="line"><span class="comment"># </span></span><br><span class="line"><span class="comment">#       server &#123;</span></span><br><span class="line"><span class="comment">#               listen     localhost:110;</span></span><br><span class="line"><span class="comment">#               protocol   pop3;</span></span><br><span class="line"><span class="comment">#               proxy      on;</span></span><br><span class="line"><span class="comment">#       &#125;</span></span><br><span class="line"><span class="comment"># </span></span><br><span class="line"><span class="comment">#       server &#123;</span></span><br><span class="line"><span class="comment">#               listen     localhost:143;</span></span><br><span class="line"><span class="comment">#               protocol   imap;</span></span><br><span class="line"><span class="comment">#               proxy      on;</span></span><br><span class="line"><span class="comment">#       &#125;</span></span><br><span class="line"><span class="comment">#&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-2-2-修改index-html"><a href="#2-2-2-修改index-html" class="headerlink" title="2.2.2 修改index.html"></a>2.2.2 修改index.html</h4><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /usr/share/nginx/html/index.html</span><br></pre></td></tr></table></figure>
<p>输入以下内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;<span class="built_in">head</span>&gt;</span><br><span class="line">&lt;title&gt;Welcome to nginx!&lt;/title&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">    body &#123;</span><br><span class="line">        width: 35em;</span><br><span class="line">        margin: 0 auto;</span><br><span class="line">        font-family: Tahoma, Verdana, Arial, sans-serif;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;h1&gt;Welcome to nginx!(192.168.126.2)&lt;/h1&gt;</span><br><span class="line">&lt;p&gt;If you see this page, the nginx web server is successfully installed and</span><br><span class="line">working. Further configuration is required.&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">&lt;p&gt;For online documentation and support please refer to</span><br><span class="line">&lt;a href=<span class="string">&quot;http://nginx.org/&quot;</span>&gt;nginx.org&lt;/a&gt;.&lt;br/&gt;</span><br><span class="line">Commercial support is available at</span><br><span class="line">&lt;a href=<span class="string">&quot;http://nginx.com/&quot;</span>&gt;nginx.com&lt;/a&gt;.&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">&lt;p&gt;&lt;em&gt;Thank you <span class="keyword">for</span> using nginx.&lt;/em&gt;&lt;/p&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h2 id="3、启动"><a href="#3、启动" class="headerlink" title="3、启动"></a>3、启动</h2><h3 id="3-1-启动nginx"><a href="#3-1-启动nginx" class="headerlink" title="3.1 启动nginx"></a>3.1 启动nginx</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx</span><br></pre></td></tr></table></figure>
<h3 id="3-2-启动keepalived"><a href="#3-2-启动keepalived" class="headerlink" title="3.2 启动keepalived"></a>3.2 启动keepalived</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">keepalived -l -f /etc/keepalived/keepalived.conf</span><br></pre></td></tr></table></figure>
<h2 id="4、状态查看"><a href="#4、状态查看" class="headerlink" title="4、状态查看"></a>4、状态查看</h2><h2 id="4-1-查看nginx状态"><a href="#4-1-查看nginx状态" class="headerlink" title="4.1 查看nginx状态"></a>4.1 查看nginx状态</h2><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service nginx status</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">* nginx is running</span><br></pre></td></tr></table></figure>
<h3 id="4-2-查看keepalived进程"><a href="#4-2-查看keepalived进程" class="headerlink" title="4.2 查看keepalived进程"></a>4.2 查看keepalived进程</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef|grep keepalived</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">root    31     1  1 07:50 ?        00:00:00 keepalived -l -f /etc/keepalived/keepalived.conf</span><br><span class="line">root    32    31  3 07:50 ?        00:00:00 keepalived -l -f /etc/keepalived/keepalived.conf</span><br><span class="line">root    33    31  4 07:50 ?        00:00:00 keepalived -l -f /etc/keepalived/keepalived.conf</span><br><span class="line">root    35    11  0 07:51 pts/1    00:00:00 grep --color=auto keepalived</span><br></pre></td></tr></table></figure>
<h3 id="4-3-查看vip挂载情况"><a href="#4-3-查看vip挂载情况" class="headerlink" title="4.3 查看vip挂载情况"></a>4.3 查看vip挂载情况</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ip a</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</span><br><span class="line">    <span class="built_in">link</span>/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1/8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">24: eth0@if25: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether 02:42:c0:a8:7e:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0</span><br><span class="line">    inet 192.168.126.2/24 brd 192.168.126.255 scope global eth0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet 192.168.200.16/32 scope global eth0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>

<h2 id="4、停止"><a href="#4、停止" class="headerlink" title="4、停止"></a>4、停止</h2><h3 id="4-1-停止nginx"><a href="#4-1-停止nginx" class="headerlink" title="4.1 停止nginx"></a>4.1 停止nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx -s stop</span><br></pre></td></tr></table></figure>
<h3 id="4-2-停止keepalived"><a href="#4-2-停止keepalived" class="headerlink" title="4.2 停止keepalived"></a>4.2 停止keepalived</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pkill keepalived</span><br></pre></td></tr></table></figure>

<h1 id="三、从机"><a href="#三、从机" class="headerlink" title="三、从机"></a>三、从机</h1><h2 id="1、方法一"><a href="#1、方法一" class="headerlink" title="1、方法一"></a>1、方法一</h2><h3 id="1-1-重复主机环境步骤"><a href="#1-1-重复主机环境步骤" class="headerlink" title="1.1 重复主机环境步骤"></a>1.1 重复主机环境步骤</h3><h3 id="1-2-将步骤-1-2-创建网桥的IP地址改成其他段"><a href="#1-2-将步骤-1-2-创建网桥的IP地址改成其他段" class="headerlink" title="1.2 将步骤 1.2 创建网桥的IP地址改成其他段"></a>1.2 将步骤 1.2 创建网桥的IP地址改成其他段</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker network create -d=bridge --subnet=192.168.127.0/24 br1</span><br></pre></td></tr></table></figure>
<h3 id="1-3-将步骤2-1-配置keepalived内容改为如下"><a href="#1-3-将步骤2-1-配置keepalived内容改为如下" class="headerlink" title="1.3 将步骤2.1 配置keepalived内容改为如下"></a>1.3 将步骤2.1 配置keepalived内容改为如下</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">! Configuration File <span class="keyword">for</span> keepalived</span><br><span class="line"></span><br><span class="line">global_defs &#123;</span><br><span class="line">   <span class="comment">#路由id：当前安装keepalived节点主机的标识符，全局唯一</span></span><br><span class="line">   router_id keep_131</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    <span class="comment"># 表示的状态，当前的130服务器为nginx的主节点，MASTER/BACKUP</span></span><br><span class="line">    state BACKUP</span><br><span class="line">    <span class="comment"># 当前实例绑定的网卡</span></span><br><span class="line">    interface eth0</span><br><span class="line">    <span class="comment"># 保证主备节点一致</span></span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    <span class="comment"># 优先级/权重，谁的优先级高，在MASTER挂掉以后，就能成为MASTER</span></span><br><span class="line">    priority 80</span><br><span class="line">    <span class="comment"># 主备之间同步检查的时间间隔，默认1s</span></span><br><span class="line">    advert_int 1</span><br><span class="line">    <span class="comment"># 认证授权的密码，防止非法节点的进入</span></span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.200.16</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2、方法二（推荐！省事）"><a href="#2、方法二（推荐！省事）" class="headerlink" title="2、方法二（推荐！省事）"></a>2、方法二（推荐！省事）</h2><h3 id="2-1-将主机虚拟机关机完整克隆一份作为从机"><a href="#2-1-将主机虚拟机关机完整克隆一份作为从机" class="headerlink" title="2.1 将主机虚拟机关机完整克隆一份作为从机"></a>2.1 将主机虚拟机关机完整克隆一份作为从机</h3><h3 id="2-2-修改keepalived的配置"><a href="#2-2-修改keepalived的配置" class="headerlink" title="2.2 修改keepalived的配置"></a>2.2 修改keepalived的配置</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/keepalived/keepalived.conf</span><br></pre></td></tr></table></figure>
<p>内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">! Configuration File <span class="keyword">for</span> keepalived</span><br><span class="line"></span><br><span class="line">global_defs &#123;</span><br><span class="line">   <span class="comment">#路由id：当前安装keepalived节点主机的标识符，全局唯一</span></span><br><span class="line">   router_id keep_131</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    <span class="comment"># 表示的状态，当前的130服务器为nginx的主节点，MASTER/BACKUP</span></span><br><span class="line">    state BACKUP</span><br><span class="line">    <span class="comment"># 当前实例绑定的网卡</span></span><br><span class="line">    interface eth0</span><br><span class="line">    <span class="comment"># 保证主备节点一致</span></span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    <span class="comment"># 优先级/权重，谁的优先级高，在MASTER挂掉以后，就能成为MASTER</span></span><br><span class="line">    priority 50</span><br><span class="line">    <span class="comment"># 主备之间同步检查的时间间隔，默认1s</span></span><br><span class="line">    advert_int 1</span><br><span class="line">    <span class="comment"># 认证授权的密码，防止非法节点的进入</span></span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.200.16</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-3-启动keepalived服务"><a href="#2-3-启动keepalived服务" class="headerlink" title="2.3 启动keepalived服务"></a>2.3 启动keepalived服务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">keepalived -l -f /etc/keepalived/keepalived.conf</span><br></pre></td></tr></table></figure>
<h3 id="2-4-启动nginx服务"><a href="#2-4-启动nginx服务" class="headerlink" title="2.4 启动nginx服务"></a>2.4 启动nginx服务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx</span><br></pre></td></tr></table></figure>

<hr>
注：<b>keepalived过程中出现的其他问题，详见<a target="_blank" rel="noopener" href="https://blog.csdn.net/lxcw_sir/article/details/134688834">
<font color="blue"><u>
🔗在docker的ubuntu中安装keepalived
</u></font></a><br>
<hr>

<h1 id="四、路由环境"><a href="#四、路由环境" class="headerlink" title="四、路由环境"></a>四、路由环境</h1><p>具体路由如下图<br><img src="https://img-blog.csdnimg.cn/direct/d461438f337544b586c9060a8f7dda3d.png#pic_center" alt="主要路由环境"></p>
<h2 id="1、主机"><a href="#1、主机" class="headerlink" title="1、主机"></a>1、主机</h2><h3 id="1-1-网卡情况"><a href="#1-1-网卡情况" class="headerlink" title="1.1 网卡情况"></a>1.1 网卡情况</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ip a</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</span><br><span class="line">    <span class="built_in">link</span>/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1/8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1/128 scope host </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel state UP group default qlen 1000</span><br><span class="line">    <span class="built_in">link</span>/ether 00:0c:29:b6:5d:6c brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    altname enp2s1</span><br><span class="line">    inet 192.168.100.157/24 brd 192.168.100.255 scope global dynamic noprefixroute ens33</span><br><span class="line">       valid_lft 59332sec preferred_lft 59332sec</span><br><span class="line">    inet6 fe80::f040:fdbe:78e1:5077/64 scope <span class="built_in">link</span> noprefixroute </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">3: docker0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default </span><br><span class="line">    <span class="built_in">link</span>/ether 02:42:65:2d:ef:09 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::42:65ff:fe2d:ef09/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">18: br-5485ae4ce244: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether 02:42:46:41:1a:20 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.126.1/24 brd 192.168.126.255 scope global br-5485ae4ce244</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::42:46ff:fe41:1a20/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">25: vethd5c0922@if24: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br-5485ae4ce244 state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether a2:75:3c:3c:0d:62 brd ff:ff:ff:ff:ff:ff link-netnsid 0</span><br><span class="line">    inet6 fe80::a075:3cff:fe3c:d62/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">27: vethec20693@if26: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br-4bd1ee90e211 state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether 92:80:a3:b4:06:4a brd ff:ff:ff:ff:ff:ff link-netnsid 1</span><br><span class="line">    inet6 fe80::9080:a3ff:feb4:64a/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>

<h3 id="1-2-查看路由"><a href="#1-2-查看路由" class="headerlink" title="1.2 查看路由"></a>1.2 查看路由</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route -n</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">0.0.0.0         192.168.100.1   0.0.0.0         UG    100    0        0 ens33</span><br><span class="line">169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33</span><br><span class="line">172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0</span><br><span class="line">192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33</span><br><span class="line">192.168.126.0   0.0.0.0         255.255.255.0   U     0      0        0 br-5485ae4ce244</span><br></pre></td></tr></table></figure>
<h3 id="1-3-添加路由"><a href="#1-3-添加路由" class="headerlink" title="1.3 添加路由"></a>1.3 添加路由</h3><p>主机到从机的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.127.0/24 gw 192.168.100.158</span><br></pre></td></tr></table></figure>
<p>主机到vip的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.200.0/24 gw 192.168.126.2</span><br></pre></td></tr></table></figure>

<p>vip到从机的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.200.0/24 gw 192.168.100.158</span><br></pre></td></tr></table></figure>
<h3 id="1-4-最后路由表"><a href="#1-4-最后路由表" class="headerlink" title="1.4 最后路由表"></a>1.4 最后路由表</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route -n</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">0.0.0.0         192.168.100.1   0.0.0.0         UG    100    0        0 ens33</span><br><span class="line">169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33</span><br><span class="line">172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0</span><br><span class="line">192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33</span><br><span class="line">192.168.126.0   0.0.0.0         255.255.255.0   U     0      0        0 br-5485ae4ce244</span><br><span class="line">192.168.127.0   192.168.100.158 255.255.255.0   UG    0      0        0 ens33</span><br><span class="line">192.168.200.0   192.168.126.2   255.255.255.0   UG    0      0        0 br-5485ae4ce244</span><br><span class="line">192.168.200.0   192.168.100.158 255.255.255.0   UG    0      0        0 ens33</span><br></pre></td></tr></table></figure>

<h2 id="2、从机"><a href="#2、从机" class="headerlink" title="2、从机"></a>2、从机</h2><h3 id="2-1-网卡情况"><a href="#2-1-网卡情况" class="headerlink" title="2.1 网卡情况"></a>2.1 网卡情况</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ip a</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</span><br><span class="line">    <span class="built_in">link</span>/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1/8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1/128 scope host </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel state UP group default qlen 1000</span><br><span class="line">    <span class="built_in">link</span>/ether 00:0c:29:6a:2e:27 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    altname enp2s1</span><br><span class="line">    inet 192.168.100.158/24 brd 192.168.100.255 scope global dynamic noprefixroute ens33</span><br><span class="line">       valid_lft 64476sec preferred_lft 64476sec</span><br><span class="line">    inet6 fe80::5d22:1c65:d887:63e6/64 scope <span class="built_in">link</span> noprefixroute </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">3: docker0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default </span><br><span class="line">    <span class="built_in">link</span>/ether 02:42:69:89:<span class="built_in">fc</span>:8c brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::42:69ff:fe89:fc8c/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">4: br-c0c4641ba16b: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether 02:42:a3:ee:77:97 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.127.1/24 brd 192.168.127.255 scope global br-c0c4641ba16b</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::42:a3ff:feee:7797/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">10: vethbe7b7fe@if9: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue master br-c0c4641ba16b state UP group default </span><br><span class="line">    <span class="built_in">link</span>/ether 1a:e7:2f:a9:3b:8e brd ff:ff:ff:ff:ff:ff link-netnsid 0</span><br><span class="line">    inet6 fe80::18e7:2fff:fea9:3b8e/64 scope <span class="built_in">link</span> </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>
<h3 id="2-2-查看路由"><a href="#2-2-查看路由" class="headerlink" title="2.2 查看路由"></a>2.2 查看路由</h3><p>输入命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route -n</span><br></pre></td></tr></table></figure>
<p>打印返回</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">0.0.0.0         192.168.100.1   0.0.0.0         UG    100    0        0 ens33</span><br><span class="line">169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33</span><br><span class="line">172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0</span><br><span class="line">192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33</span><br><span class="line">192.168.127.0   0.0.0.0         255.255.255.0   U     0      0        0 br-c0c4641ba16b</span><br></pre></td></tr></table></figure>
<h3 id="2-3-添加路由"><a href="#2-3-添加路由" class="headerlink" title="2.3 添加路由"></a>2.3 添加路由</h3><p>从机到主机的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.126.0/24 gw 192.168.100.157</span><br></pre></td></tr></table></figure>
<p>从机到vip的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.200.0/24 gw 192.168.127.2</span><br></pre></td></tr></table></figure>

<p>vip到主机的路由</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add -net 192.168.200.0/24 gw 192.168.100.157</span><br></pre></td></tr></table></figure>
<h3 id="2-4-最后路由情况"><a href="#2-4-最后路由情况" class="headerlink" title="2.4 最后路由情况"></a>2.4 最后路由情况</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route -n</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Kernel IP routing table</span><br><span class="line">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</span><br><span class="line">0.0.0.0         192.168.100.1   0.0.0.0         UG    100    0        0 ens33</span><br><span class="line">169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33</span><br><span class="line">172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0</span><br><span class="line">192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33</span><br><span class="line">192.168.126.0   192.168.100.157 255.255.255.0   UG    0      0        0 ens33</span><br><span class="line">192.168.127.0   0.0.0.0         255.255.255.0   U     0      0        0 br-c0c4641ba16b</span><br><span class="line">192.168.200.0   192.168.126.2   255.255.255.0   UG    0      0        0 br-c0c4641ba16b</span><br><span class="line">192.168.200.0   192.168.100.157 255.255.255.0   UG    0      0        0 ens33</span><br></pre></td></tr></table></figure>

<h2 id="3、宿主机（windows主机）"><a href="#3、宿主机（windows主机）" class="headerlink" title="3、宿主机（windows主机）"></a>3、宿主机（windows主机）</h2><h3 id="3-1-添加路由"><a href="#3-1-添加路由" class="headerlink" title="3.1 添加路由"></a>3.1 添加路由</h3><p>vip到主机</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add 192.168.200.0 mask 255.255.255.0 192.168.100.157</span><br></pre></td></tr></table></figure>
<p>vip到从机</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">route add 192.168.200.0 mask 255.255.255.0 192.168.100.158</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://lxcwlong.gitee.io/lxcwlong/2023/12/25/docker-ubuntu%E4%B8%AD%E5%9F%BA%E4%BA%8Ekeepalived+niginx%E6%A8%A1%E6%8B%9F%E4%B8%BB%E4%BB%8E%E7%83%AD%E5%A4%87%E5%AE%8C%E6%95%B4%E8%BF%87%E7%A8%8B/" data-id="clqkjj81t00009wwk8qip9a81" data-title="docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/lxcwlong/2023/12/25/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/lxcwlong/archives/2023/12/">December 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/lxcwlong/2023/12/25/docker-ubuntu%E4%B8%AD%E5%9F%BA%E4%BA%8Ekeepalived+niginx%E6%A8%A1%E6%8B%9F%E4%B8%BB%E4%BB%8E%E7%83%AD%E5%A4%87%E5%AE%8C%E6%95%B4%E8%BF%87%E7%A8%8B/">docker-ubuntu中基于keepalived+niginx模拟主从热备完整过程</a>
          </li>
        
          <li>
            <a href="/lxcwlong/2023/12/25/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 Wcloong<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/lxcwlong/" class="mobile-nav-link">Home</a>
  
    <a href="/lxcwlong/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/lxcwlong/js/jquery-3.6.4.min.js"></script>



  
<script src="/lxcwlong/fancybox/jquery.fancybox.min.js"></script>




<script src="/lxcwlong/js/script.js"></script>





  </div>
</body>
</html>